package view.mediator
{
	import controllers.commands.elecboundary.SubmitEleBoundaryProtocalCommand;
	import controllers.commands.base.ICommand;
	import controllers.commands.elecboundary.CancelElectricBoundaryCommand;
	import controllers.commands.elecboundary.DrawBoundaryCommand;
	import controllers.commands.elecboundary.DriveInOutAlertCommand;
	import controllers.managers.SingletonManager;
	
	import event.ElectricBoundaryEvent;
	
	import model.context.ElecBoundaryContext;
	import model.context.StartEleBoundaryProtocalContext;
	import model.context.base.IContext;
	import model.vo.ProtocalEleBoundary;
	
	import mx.core.UIComponent;
	
	import view.mediator.base.IMediator;
	import view.panel.ElectricBoundaryToolbar;
	
	/**
	 *  工具栏代理
	 **/
	public class ElectricBoundaryToolMediator implements IMediator
	{
		private var toolbar : ElectricBoundaryToolbar = null;
		private var cmd : ICommand = null;
		private var context : ElecBoundaryContext = null;
		private var gContext : StartEleBoundaryProtocalContext = null;
		
		public function ElectricBoundaryToolMediator()
		{
		}
				
		public function setContext( cxt:IContext ):void
		{
			context = ElecBoundaryContext(cxt );
		}
		
		public function initEvents():void
		{}
		
		public function setView(view:UIComponent):void
		{
			toolbar = ElectricBoundaryToolbar(view);
			toolbar.addEventListener(ElectricBoundaryEvent.CLOSE_TOOLBAR, onCloseToolbarHandler);
			toolbar.addEventListener(ElectricBoundaryEvent.DELETE, onDeleteBoundaryHandler);
			toolbar.addEventListener(ElectricBoundaryEvent.DRAW, onDrawBoundaryHandler);
			toolbar.addEventListener(ElectricBoundaryEvent.DRIVE_IN_OUT, onDriveInOutHandler);
			
			toolbar.addEventListener(ElectricBoundaryEvent.SAVE, onSaveBoundaryHandler);
			toolbar.addEventListener(ElectricBoundaryEvent.CLEAR, onClearBoundaryHandler);
			
			gContext = StartEleBoundaryProtocalContext(SingletonManager.getServ(StartEleBoundaryProtocalContext.NAME));
			
			//默认设置
			gContext.protocalType = ProtocalEleBoundary.SET_BOUNDARY;
		}
		
		/**
		 *  保存当前电子围栏，提交协议
		 * */ 
		private function onSaveBoundaryHandler(evt:ElectricBoundaryEvent):void
		{
			cmd = new SubmitEleBoundaryProtocalCommand();
			cmd.setContext(gContext);
			cmd.execute();
		}
		
		/**
		 *  清除电子围栏.
		 **/
		private function onClearBoundaryHandler(evt:ElectricBoundaryEvent):void
		{
			this.context.map.clearOverlays();
		}
		
		private function onCloseToolbarHandler(evt:ElectricBoundaryEvent):void
		{
			if( toolbar != null )
			{
				toolbar.visible = false;
			}
			
			trace("onCloseToolbarHandler");
		}
		
		private function onDeleteBoundaryHandler(evt:ElectricBoundaryEvent):void
		{
			gContext.protocalType = ProtocalEleBoundary.CANCEL_BOUNDARY;
		}
		
		private function onDrawBoundaryHandler(evt:ElectricBoundaryEvent):void
		{
			gContext.protocalType = ProtocalEleBoundary.SET_BOUNDARY;
//			cmd  = new DrawBoundaryCommand();
//			cmd.setContext({});
//			cmd.execute();
		}
		
		private function onDriveInOutHandler(evt:ElectricBoundaryEvent):void
		{
			if ( evt.isDriveIn )
			{
				gContext.protocalType = ProtocalEleBoundary.DRIVE_IN_ALERT;
			}
			else
			{
				gContext.protocalType = ProtocalEleBoundary.DRIVE_OUT_ALERT;
			}
				 
//			cmd  = new DriveInOutAlertCommand();
//			cmd.setContext({});
//			cmd.execute();
		}
	}
}